package com.jnxg.smartcommunity.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import com.jnxg.smartcommunity.fragment.BaseFragment;
import com.jnxg.smartcommunity.listener.FragmentListener;


public class UIUtils {
	public static void addOnGlobalLayoutListener(View view,
			OnGlobalLayoutListener listener) {
		ViewTreeObserver vto = view.getViewTreeObserver();
		vto.addOnGlobalLayoutListener(listener);
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public static void removeOnGlobalLayoutListener(View view,
			OnGlobalLayoutListener listener) {
		ViewTreeObserver vto = view.getViewTreeObserver();
		if (Build.VERSION.SDK_INT < 16) {
			vto.removeGlobalOnLayoutListener(listener);
		} else {
			vto.removeOnGlobalLayoutListener(listener);
		}
	}
	
	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public static void setBackground(View view,
			Drawable drawable) {
		if (Build.VERSION.SDK_INT < 16) {
			view.setBackgroundDrawable(drawable);
		} else {
			view.setBackground(drawable);
		}
	}

	public static int getStatusBarHeight(Activity activity) {
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		return statusBarHeight;
	}

	public static Bitmap toRoundCorner(Bitmap bitmap, float pixels) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = pixels;
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}
	
	public static void waitFragmentAdded(final BaseFragment fragment,
			final Activity activity, final FragmentListener listener){
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (!fragment.isAdded()) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
					}
				}
				activity.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						listener.onFragmentAdded(fragment);
					}
				});
			}
		}).start();
	}
	
	
	public static int getFontSizeByMaxHeight(double maxHeight, Context context){
		int font = 1;
		while (true) {
			Paint paint = new Paint();  
		    paint.setTextSize(font);  
		    FontMetrics fm = paint.getFontMetrics();  
		    if(maxHeight < Math.ceil(fm.descent - fm.ascent))
		    	break;
		    font++;
		}
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 
        return (int) (font / fontScale + 0.5f); 
	}
	
	public static int getFontSizeByMaxWidth(String str, double maxWidth, Context context){
		int font = 1;
		while (true) {
			Paint paint = new Paint();  
		    paint.setTextSize(font);  
		    Rect rect = new Rect();
		    paint.getTextBounds(str, 0, str.length(), rect); 
		    if(maxWidth < rect.width())
		    	break;
		    font++;
		}
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 
        return (int) (font / fontScale - 0.5f); 
	}
}
